package com.study.api;

/**
 * @description：后端返回数据封装类
 * @author：李军荣
 * @date: 2020/12/22 21:13
 */
public class Result<T> {

    private final int code;
    private final String msg;
    private final T data;

    private Result(Builder<T> builder){
        code = builder.code;
        msg = builder.msg;
        data = builder.data;
    }

    public static class Builder<T>{
        private int code;
        private String msg;
        private T data;

        public Builder(){};

        public Builder customize(int code,String msg){
            this.code=code;
            this.msg=msg;
            return this;
        }

        public Builder customize(String msg){
            this.code=-1;
            this.msg=msg;
            return this;
        }

        public Builder ok(){
            this.code=200;
            this.msg="success";
            return this;
        }

        public Builder fail(){
            this.code=-1;
            this.msg="failed";
            return this;
        }

        public Builder data(T data){
            this.data=data;
            return this;
        }

        public Result bulider(){
            return new Result(this);
        }
    }


    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
